package cn.edu.nju.ws.sview.wyge.servlets;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import cn.edu.nju.ws.sview.views.Group;
import cn.edu.nju.ws.sview.wyge.relations.adataset.relations.Relation;

/**
 * Servlet implementation class Entity
 */
public class RelationIntoSession extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RelationIntoSession() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String json = null;
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		if (request.getParameter("data") != null) {
			json = request.getParameter("data");
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			int id = Integer.parseInt((String) regtJSON.get("id"));
			Relation rel = null;
			if (id != 0) {
				rel = Relation.getInstance(id);
			} else {
//				int gid = Integer.valueOf((String) regtJSON.get("gid"));
				String groupName = (String)session.getAttribute("groupName");
				int gid = Group.getGroupIdByName(groupName);
				rel = Relation.getInstanceNew(gid);
			}

			if (session.getAttribute("relation" + id) != null) {
				session.removeAttribute("relation" + id);
			}
			do {
				session.setAttribute("relation" + id, rel);
			} while (session.getAttribute("relation" + id) == null);
			JSONObject result = new JSONObject();
			result.put("result", "ok");
			response.getWriter().print(result);
		}else if(request.getParameter("translateNew") != null){
			json = request.getParameter("translateNew");
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			int old_id = Integer.parseInt((String)regtJSON.get("old_id"));
			String lang = (String) regtJSON.get("olang");
			Relation relOld = Relation.getInstance(old_id);
			Relation relNew = Relation.getInstanceTranslate(relOld, lang);
			int new_id = relNew.getRelationID();
			if(session.getAttribute("relation"+new_id) != null){
				session.removeAttribute("relation"+new_id);
			}
			do{
				session.setAttribute("relation"+new_id, relNew);
			}while(session.getAttribute("relation"+new_id) == null);
			JSONObject result = new JSONObject();
			result.put("result", "ok");
			result.put("new_id", new_id);
			response.getWriter().print(result);
		}else if(request.getParameter("translateGet") != null){
			json = request.getParameter("translateGet");
			String str = URLDecoder.decode(json, "UTF-8");
			JSONObject regtJSON = (JSONObject) JSONValue.parse(str);
			int new_id = Integer.parseInt((String)regtJSON.get("new_id"));
			String lang = (String) regtJSON.get("olang");
			Relation relNew = null;
			if(new_id == 0){
				int old_id = Integer.parseInt((String)regtJSON.get("old_id"));
				Relation relOld = Relation.getInstance(old_id);
				relNew = Relation.getInstanceTranslate(relOld, lang);
			}else{
				relNew = Relation.getInstance(new_id);
			}
			if(relNew == null){
				System.err.println("error in RelationIntoSession! relNew=null");
			}
			if(session.getAttribute("relation"+new_id) != null){
				session.removeAttribute("relation"+new_id);
			}
			do{
				session.setAttribute("relation"+new_id, relNew);
			}while(session.getAttribute("relation"+new_id) == null);
			
			JSONObject result = new JSONObject();
			result.put("result", "ok");
			result.put("new_id", new_id);
			response.getWriter().print(result);
		}
	}
}
